package com.example.design.Copy;

import lombok.Builder;
import lombok.Data;

import java.io.*;

@Data
@Builder
public class Teacher implements Serializable {

    private String teacherName;

    private Student student;

    public Teacher copy(){
        ObjectOutputStream objectOutputStream = null;
        ObjectInputStream objectInputStream = null;
        Teacher teacher = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
            //将流序列化成对象
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            objectInputStream = new ObjectInputStream(inputStream);
            teacher = (Teacher) objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return teacher;
    }
}
